<?php

class PageController extends Controller
{
	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model=new Page('search');
		$model->unsetAttributes();  // clear any default values

		$criteria = new CDbCriteria;
		if(isset($_GET['Page']))
		{
			$this->pageTitle = 'Pages search result - ' . Yii::app()->name;
			
			$model->title = $_GET['Page']['title'];
			$criteria->addSearchCondition('title', $_GET['Page']['title'], true, 'OR');
			$criteria->addSearchCondition('content', $_GET['Page']['title'], true, 'OR');
			$criteria->mergeWith($this->getCriteria());
			$keyword = $model->title;
		}
		else
		{
			$this->pageTitle = 'Pages - ' . Yii::app()->name;
			$keyword = false;
		}
		
		$dataProvider = new CActiveDataProvider('Page', array(
			'criteria'=>$criteria
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
			'model'=>$model,
			'keyword' =>$keyword,
		));
	}	

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model = $this->loadModel($id);
		$this->pageTitle = $model->title . ' - ' . Yii::app()->name;

		$this->render('view',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Page the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Page::model()->findByPk($id, $this->getCriteria());
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	protected function getCriteria()
	{
		$criteria = new CDbCriteria;
		$criteria->addCondition('visible = :visible');
		$criteria->addCondition('del_flg = :del_flg');
		$criteria->params = array(
			':visible'=>Yii::app()->params['VISIBLE_ON'],
			':del_flg'=>Yii::app()->params['DEL_FLG_OFF'],
		);
		return $criteria;
	}
}
